var _imageList = [];
var hnd; //定时器对象


//加载图片预览
function _loadImageShow(){
	
	var layerShow = new _ImageShow();
	layerShow.getImageList();
	layerShow.clickBind();
}

//更新ImageList
function _updateImageList(){
	var layerShow = new _ImageShow();
	layerShow.getImageList();
	layerShow.imageClickBind();
}

//js插件 图片预览

function _ImageShow() {
}

_ImageShow.prototype = {

	//图片点击预览绑定
	imageClickBind:function(){
		var $this = this;
		$("."+Constants._layerShow).each(function(i) {
			
			if($(this).attr("src") !=null && $(this).attr("src")!=""){
				
				$(this).click(function() {
					
						$("#imagePreview").find("img").attr("src", _imageList[i]);
						$("#imagePreview").find("img").attr("data-index", i);
						$this.layerOpen();
					});			
			}
			
		});
	},
		
	//绑定事件
	clickBind : function() {
		var $this = this;

		$this.imageClickBind();
		
		//点击图片弹框预览		
		
		$("#imagePreview #taskTime").click(function() {
			var state = $("#imagePreview #taskTime").attr("data-state");
			if (state == "close") {
				$("#imagePreview #taskTime").text(zLocale.public_imageshow_endtask);
				$("#imagePreview #taskTime").css("color","#1aa1b5");
				$("#imagePreview #taskTime").attr("data-state", "open");

				hnd = setInterval(function() {
					$this.myInterval();
				}, 1500); //1000为1秒钟
			} else if (state == "open") {
				$("#imagePreview #taskTime").text(zLocale.public_imageshow_starttask);
				$("#imagePreview #taskTime").css("color","#ccc");
				$("#imagePreview #taskTime").attr("data-state", "close");

				clearInterval(hnd);
			}
		});

		//左右手指切换图片
		$("#imagePreview #left").click(function() {
			
			var index = $("#imagePreview").find("img").attr("data-index");
			var oldindex=index;
			if (index > 0) {
				index--;
			} else if (index <= 0) {
				index = _imageList.length - 1;
			}
			
			if(oldindex ==index){
				return;
			}
			$("#imagePreview").find("img").attr("src", _imageList[index]);
			$("#imagePreview").find("img").attr("data-index", index);
		
			$this.imageCenter();
		});

		$("#imagePreview #right").click(function() {
			
			var index = $("#imagePreview").find("img").attr("data-index");
			var oldindex=index;
			if (index >= _imageList.length - 1) {
				index = 0;
			} else if (index < _imageList.length - 1) {
				index++;
			}
			
			if(oldindex ==index){
				return;
			}
			$("#imagePreview").find("img").attr("src", _imageList[index]);
			$("#imagePreview").find("img").attr("data-index", index);
			
			$this.imageCenter();
		});

		//悬浮到指定区域显示手指
		$("#imagePreview #left,#imagePreview #right").mouseover(function() {
			$(this).find('.hand').css("display", "block");
		});
		
		$("#imagePreview #left,#imagePreview #right").mouseout(function() {
			$(this).find('.hand').css("display", "none");
		});

	},

	//获取图片列表
	getImageList : function() {
		//var pic=document.getElementById("father").getElementsByTagName("img");
		_imageList=[];
		var pic = document.getElementsByClassName(Constants._layerShow);
		for (var i = 0; i < pic.length; i++) {
			var src=pic[i].getAttribute("src");
			
				_imageList.push(src);			
		}
		var imgsrc = _imageList[0];
		$("#imagePreview").find("img").attr("src", _imageList[0]);
		$("#imagePreview").find("img").attr("data-index", 0);

	},

	//图片弹框
	layerOpen : function() {
		var $this = this;
		// 关闭弹框
		layer.closeAll();
		layer.open({
			type : 1,
			title: zLocale.public_imageshow,
			area : [ '800px', '520px' ],
			content : $("#imagePreview"),
			success : function(layero, index) {
				
				$this.imageCenter();
				
			},
			end : function() {
				$("#imagePreview #taskTime").text(zLocale.public_imageshow_starttask);
				$("#imagePreview #taskTime").css("color","#ccc");
				$("#imagePreview #taskTime").attr("data-state", "close");
				clearInterval(hnd);
				
			}
		});

	},

	//图片居中显示
	imageCenter: function(){
		var img_h=$("#imagePreview").find("img").height();
		var _h=$("#imagePreview #left").height();
		if(!isNaN(img_h) && !isNaN(_h)){
			var number=_h-img_h;
			if(number > 0){
				$("#imagePreview").find("img").css("margin-top",(number/2)+"px");
			}else{
				$("#imagePreview").find("img").css("margin-top",+"");
			}					
		}
	},
	
	//task任务
	myInterval : function() {
		var $this = this;
		
		var index = $("#imagePreview").find("img").attr("data-index");
		var oldindex=index;
		if (index >= _imageList.length - 1) {
			index = 0;
		} else if (index < _imageList.length - 1) {
			index++;
		}
		
		if(oldindex ==index){
			return;
		}
		$("#imagePreview").find("img").attr("src", _imageList[index]);
		$("#imagePreview").find("img").attr("data-index", index);
		
		$this.imageCenter();
	}
}